﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using mshtml;
using ConfigLibrary;
using System.Web;
using System.Collections.Specialized;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;

namespace Remodel
{
    
    public partial class eddsform : Form
    {
        
        WebBrowser edds = new WebBrowser();        
        public static observationlog observationpage;
        DataTable tridenttable;        
        public static HtmlElement eddsemail;
        public static HtmlElement eddspw;
        public static HtmlElement chkbox;
        public static HtmlElement eddsbutton;
        public static HtmlElement agingreport;
        public static HtmlElement form;
        public static HtmlElement conf;
        bool loggedin = false;      
        DateTime dt = new DateTime();   
        string sid;
        private Config config;
        public static string email;
        public static string password;
        private string host;
        private int port;
        int tr;
        Excel.Worksheet spockws;
        
        public eddsform(Excel.Worksheet wsc)
        {
            spockws = wsc;
            InitializeComponent();
            emailpassword();
        }

        private void edds_Load(object sender, EventArgs e)
        {      
            edds.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(eddswebpage_DocumentCompleted);            
            edds.AllowNavigation = true;
            edds.Navigate("https://www.bldgportal.com/Login.aspx", false); 
        }        

        private void eddswebpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (loggedin == false)
            {
                eddsemail = edds.Document.GetElementById("UserNameTextBox");
                eddsemail.SetAttribute("value", email); 
                eddspw = edds.Document.GetElementById("PasswordTextBox");
                eddspw.SetAttribute("value", password);
                conf = edds.Document.GetElementById("ConfCheckBox");
                conf.SetAttribute("checked", "checked");
                eddsbutton = edds.Document.GetElementById("LoginButton");
                eddsbutton.InvokeMember("click");
                loggedin = true;
                             
            }
            else if (loggedin == true)
            {                
                observationpage = new observationlog(spockws, edds);
                observationpage.ShowDialog();
                this.Close();
            }
        }

        public void emailpassword()
        {
            Stream s = File.Open(Properties.Settings.Default.CONFIG, FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            config = (Config)b.Deserialize(s);
            s.Close();
            config.GetMailInfo(out host, out email, out password, out port);
        }        
    }
}
